package config;



import org.springframework.web.filter.DelegatingFilterProxy;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import javax.servlet.*;
import java.util.EnumSet;

public class WebDispatcher extends AbstractAnnotationConfigDispatcherServletInitializer {
	
	

	/** 获取Spring应用容器的配置文件 */
	@Override
	protected Class<?>[] getRootConfigClasses() {
		return null;
	}

	/** 负责获取Spring MVC应用容器，这里传入预先定义好的MyWebConfig.class */
	@Override
	protected Class<?>[] getServletConfigClasses() {
		return new Class[] { SpringConfiguration.class };
	}

	/**
	 * 负责指定需要由DispatcherServlet映射的路径，
	 * 这里给定的是"/"，意思是由DispatcherServlet处理所有向该应用发起的请求
	 * 
	 */
	@Override
	protected String[] getServletMappings() {
		return new String[] { "/" };
	}
	//添加注册其它servlet
	@Override
    public void onStartup(ServletContext servletContext) throws ServletException {
	   super.onStartup(servletContext);
		DelegatingFilterProxy delegatingFilterProxy = new DelegatingFilterProxy();
		delegatingFilterProxy.setTargetFilterLifecycle(true);

	    FilterRegistration.Dynamic shirofilter = servletContext.addFilter("shiroFilter",delegatingFilterProxy);
		shirofilter.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.INCLUDE), false, "/*");

		}
//	/*
//	 * 注册过滤器，映射路径与DispatcherServlet一致，路径不一致的过滤器需要注册到另外的WebApplicationInitializer中
//	 */
//	@Override
//	protected Filter[] getServletFilters() {
//		DelegatingFilterProxy shirofilter = new DelegatingFilterProxy();
//		shirofilter.setTargetFilterLifecycle(true);
//		return new Filter[] {shirofilter};
//	}
}